using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using DevExpress.Utils;
using DevExpress.Data.PivotGrid;
using Marlimer.Common;
using Marlimer.Logical.Comercial;
using Marlimer.Logical.Historico;
using Marlimer.Logical;

namespace Marlimer.Comercial.Reportes.Formularios
{
    public partial class frm_AvancesVenta : Form
    {
        private lhis_AnalisisVenta lhis_analisisventa = new lhis_AnalisisVenta();
        private DataTable dtResultados;

        public frm_AvancesVenta()
        {
            InitializeComponent();
        }

        private void frm_AnalisisVenta_Load(object sender, EventArgs e)
        {
            dtpFecIni.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            dtpFecFin.Value = DateTime.Today;
            configurarPivot(); 
        }

        private void configurarPivot()
        {
            
            pvtDetalles.Fields.Clear();

            //En Filas
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.RowArea, 0, "N_CCOD_RUTA", "CCOD_RUTA", "Ruta", 50, FormatType.None, "", PivotSummaryType.Count, FormatType.None, "", true));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.RowArea, 1, "N_CNOM_VEND", "CNOM_VEND", "Vendedor", 150, FormatType.None, "", PivotSummaryType.Count, FormatType.None, "", true));
            //En Columnas
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.ColumnArea, 0, "N_DFECHA", "DFECHA", "Fecha", 60, FormatType.DateTime, "dd", PivotSummaryType.Count, FormatType.None, "", true));

            //En Filtros
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.FilterArea, 0, "N_CNOM_GRPVTA", "CNOM_GRPVTA", "Grupo Venta", 150, FormatType.None, "", PivotSummaryType.Count, FormatType.None, "", true));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.FilterArea, 1, "N_CDES_PROD", "CDES_PROD", "Producto", 150, FormatType.None, "", PivotSummaryType.Count, FormatType.None, "", true));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.FilterArea, 2, "N_CNOM_ZONA", "CNOM_ZONA", "Zona", 150, FormatType.None, "", PivotSummaryType.Count, FormatType.None, "", true));

            //Adicionales
            
            //Datos
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 0, "N_NCANT_OBJ", "NCANT_OBJ", "Cant. OBJ", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 1, "N_NSOLES_OBJ", "NSOLES_OBJ", "Soles OBJ", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0.00", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 2, "N_NCANT_REAL", "NCANT_REAL", "Cant. Real", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 3, "N_NSOLES_REAL", "NSOLES_REAL", "Soles Real", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0.00", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 4, "N_NPOR_CANT", "", "Cant %", 80, FormatType.None, "", PivotSummaryType.Custom, FormatType.Numeric, "0%", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 5, "N_NPOR_SOLES", "", "Soles %", 80, FormatType.None, "", PivotSummaryType.Custom, FormatType.Numeric, "0%", true));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 6, "N_NCANT_FALTA", "NCANT_FALTA", "Cant. para OBJ", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0", false));
            pvtDetalles.Fields.Add(Util.createPivotGridField(PivotArea.DataArea, 7, "N_NSOLES_FALTA", "NSOLES_FALTA", "Soles para OBJ", 80, FormatType.None, "", PivotSummaryType.Sum, FormatType.Numeric, "0.00", true));

        }

        private void tstGenComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = -1;
            if (sender.GetType().Name == "ToolStrip")
            {
                indice = tlbComandos.Items.IndexOf(e.ClickedItem);
            }
            else
            {
                indice = mnuComandos.DropDownItems.IndexOf(e.ClickedItem);
            }

            switch (indice)
            {
                case 0:     //nuevaBusqueda
                    {
                        nuevaBusqueda();
                        break;
                    }
                case 1:     //ejecutar busqueda
                    {
                        buscar();
                        break;
                    }
                case 2:
                    {
                        exportar();
                        break; 
                    }
            }
        }

        private void exportar()
        {
            //configurar el cuadro de dialogo
            resulOMsgError resSave = Util.showDialogSave(this, "Guardar como...", "Archivos de Excel (*.xls)|*.xls");

            if (resSave.resul)
            {
                try
                {
                    pvtDetalles.ExportToXls((string)resSave.clase);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void buscar()
        {
            dtResultados = lhis_analisisventa.getConsultaAvancesVenta(dtpFecIni.Value, dtpFecFin.Value);
            actualizarPivot(dtResultados);
        }

        private void actualizarPivot(DataTable dt)
        {
            pvtDetalles.DataSource = dt;
        }

        private void nuevaBusqueda()
        {
            pvtDetalles.DataSource = null;
            configurarPivot();
        }

        private void pvtDetalles_CustomSummary(object sender, PivotGridCustomSummaryEventArgs e)
        {
            double ncant_obj = 0;
            double nsoles_obj = 0;
            double ncant_real = 0;
            double nsoles_real = 0;

            PivotDrillDownDataSource ps = e.CreateDrillDownDataSource();
            
            if (e.DataField.Name == "N_NPOR_CANT")
            {
                //recorrer el ps
                for (int i = 0;i <ps.RowCount ; i++){
                    ncant_obj = ncant_obj + Convert.ToInt32(ps[i]["NCANT_OBJ"]);
                    ncant_real = ncant_real + Convert.ToInt32(ps[i]["NCANT_REAL"]);
                }
                e.CustomValue = (ncant_obj == 0) ? 0 : ncant_real / ncant_obj;
            }
            else if (e.DataField.Name == "N_NPOR_SOLES")
            {
                for (int i = 0; i < ps.RowCount; i++)
                {
                    nsoles_obj = nsoles_obj + Convert.ToInt32(ps[i]["NSOLES_OBJ"]);
                    nsoles_real = nsoles_real + Convert.ToInt32(ps[i]["NSOLES_REAL"]);
                }
                e.CustomValue = (nsoles_obj == 0) ? 0 : nsoles_real / nsoles_obj;
            }
        }

        
        private void pvtDetalles_CellDoubleClick(object sender, PivotCellEventArgs e)
        {
            //Cargar un formulario con los detalles
            /*frm_DetalleAnalisisVenta Frm_DetalleAnalisisVenta = new frm_DetalleAnalisisVenta(e.CreateDrillDownDataSource());
            Frm_DetalleAnalisisVenta.MdiParent = this.MdiParent; 
            Frm_DetalleAnalisisVenta.Show();              
             * */
        }


    }
}